package com.alibaba.middleware.hsf.lmbip;

import com.alibaba.lmbip.Lmbip;
import com.alibaba.middleware.hsf.Data;
import com.alibaba.middleware.hsf.guide.api.model.OrderModel;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

/**
 * @author weipeng2k 2017年12月27日 上午11:06:08
 */
@State(Scope.Benchmark)
public class DeserialPerf {

    static Lmbip lmbip = new Lmbip();

    static byte[] content;

    static OrderModel orderModel = Data.gen();

    @Setup
    public void init() {
        byte[] serialize = null;
        try {
            serialize = lmbip.serialize(orderModel);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        try {
            lmbip.deserialize(serialize);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Setup(Level.Invocation)
    public void prepare() {
        try {
            content = lmbip.serialize(orderModel);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    public void deserial() throws Exception {
        lmbip.deserialize(content);
    }

}
